# -*- coding: utf-8 -*-
"""
@author: wyb
@date: 2023/12/13
@time: 22:01
@desc: Resizes all images in current working directory to fit
in a 300x300 square, and adds catlogo.png to the lower-right corner
"""

import os
from PIL import Image

SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo2.png'
logIm = Image.open(LOGO_FILENAME)
logoWidth, logoHeight = logIm.size

os.makedirs('withLogo', exist_ok=True)
for fileName in os.listdir('.'):
    if not (fileName.endswith('.png') or fileName.endswith('.jpg')) or fileName == LOGO_FILENAME:
        continue

    im = Image.open(fileName)
    width, height = im.size
    if width > SQUARE_FIT_SIZE and height > SQUARE_FIT_SIZE:
        if width > height:
            height = int((SQUARE_FIT_SIZE / width) * height)
            width = SQUARE_FIT_SIZE
        else:
            width = int((SQUARE_FIT_SIZE / height) * width)
            height = SQUARE_FIT_SIZE
        print(f'resizing {fileName}')
        im = im.resize((width, height))

        # add logo
        print(f'add logo to {fileName} ')

        mask = Image.new("L", logIm.size, 200)
        im.paste(logIm, (width - logoWidth, height - logoHeight), mask)

        # save changes
        im.save(os.path.join('withLogo', fileName))
